package com.example.myapplication.fragment;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.example.myapplication.MyConstant;
import com.example.myapplication.R;
import com.example.myapplication.databinding.FragmentWinBinding;
import com.example.myapplication.utils.SpUtils;
import com.example.myapplication.viewmodel.MyViewModel;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.Navigation;

public class WinFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        MyViewModel myViewModel = ViewModelProviders.of(requireActivity()
                ,new SavedStateViewModelFactory(requireActivity().getApplication(),this)).get(MyViewModel.class);
        FragmentWinBinding binding = DataBindingUtil.inflate(inflater,R.layout.fragment_win,container,false);
        binding.setData(myViewModel);
        binding.setLifecycleOwner(requireActivity());

        binding.btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Navigation.findNavController(view).navigate(R.id.action_winFragment_to_titleFragment2);
            }
        });

        if (SpUtils.getBoolean(getContext(), MyConstant.TIME_COUNT,true)){
            Bundle bundle = getArguments();
            int time_count = bundle.getInt("time_count");
            binding.tvUseTime.setVisibility(View.VISIBLE);
            binding.tvUseTime.setText("使用时间："+time_count);
        }else {
            binding.tvUseTime.setVisibility(View.GONE);
        }


        Glide.with(getContext()).load(R.mipmap.applause).into(binding.imageView3);
//        MediaPlayer.create(getContext(),R.raw.success).start();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                MediaPlayer.create(getContext(),R.raw.success).start();
            }
        }, 500);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                MediaPlayer.create(getContext(),R.raw.dudu_airport).start();
            }
        }, 2500);
        return binding.getRoot();
    }
}
